package cn.com.callback

import cn.com.elite.EliteBean
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

/**
 * 平安期货AI数据拉取，插入提醒表
 */
class autoAiNotice implements CallBackInvoke {
    private static final Log log = LogFactory.getLog(autoAiNotice.class);

    public void doCallBack(EliteBean eliteBean, String callBack, Map map, String body) {

        long startTime = System.currentTimeMillis();
        try {

            //1.查询平安期货提供的AI外呼数据 dwd.ai_intention
            List SelectAIList=eliteBean.chaxun_sql("select phone_number From ai_intention","dataSource");

            for (int i=0;i<SelectAIList.size();i++){
                String Mobile=SelectAIList.get(i).get("phone_number");
                //判度提醒表里是否存在改手机号，并且是AI外呼类型的,当天存在则不插入提醒表
                List IsNoticeList=eliteBean.chaxun_sql("SELECT Mobile FROM  hst_leadsnotice where CREATEDDATE >DATE_SUB(CURDATE(),INTERVAL 0 day) and Mobile='"+Mobile+"' and AI_flag='true'","dataSource");

                   if (IsNoticeList.size()<=0){
                       //查询该条手机号是否存在促开任务
                       List OBList=eliteBean.chaxun_sql("select e.ELITETASK_GUID,e.OBJECTIVE_GUID,e.CUSTOMER_GUID,e.HANDLEBY_ID,e.HANDLEGROUP_ID  from elitetask e \n" +
                               "left join customer c on c.customer_guid=e.customer_guid \n" +
                               "where c.MOBILE='"+Mobile+"'\n" +
                               "and e.MAP_ID='4CA2PP'\n" +
                               "limit 1 ","dataSource");

                       if (OBList.size()>0){
                           String CUSTOMER_GUID=OBList.get(0).get("CUSTOMER_GUID");
                           String ELITETASK_GUID=OBList.get(0).get("ELITETASK_GUID");
                           String OBJECTIVE_GUID=OBList.get(0).get("OBJECTIVE_GUID");
                           String HANDLEBY_ID=OBList.get(0).get("HANDLEBY_ID");
                           String HANDLEGROUP_ID=OBList.get(0).get("HANDLEGROUP_ID");
                           //插入提醒表咯
                           eliteBean.update("insert into hst_leadsnotice(noticeId,noticeGroup,noticeBy,createGroup,createBy,\n" +
                                   "    noticeContent,createddate,limitCount,loadCount,noticeType,\n" +
                                   "   objective_guid,subscribedate,Customer_guid,Elitetask_guid)\n" +
                                   "   values(UUID(),'"+HANDLEGROUP_ID+"''"+HANDLEBY_ID+"','"+HANDLEGROUP_ID+"','"+HANDLEBY_ID+"',\n" +
                                   "   '您有一条AI外呼的外拨任务，请及时处理！',NOW(),1,0,'7','"+OBJECTIVE_GUID+"',\n" +
                                   "     now(),'"+CUSTOMER_GUID+"',\n" +
                                   "   '"+ELITETASK_GUID+"');","dataSource");
                       }else{
                           log.debug("该条手机号没有促开任务："+Mobile);

                       }


                   }else{
                      log.debug("该条手机号当日已经存在AI外呼拨打任务："+Mobile);
                   }




            }





        } catch (Exception e) {
            log.error("过河兵插入提醒表异常!", e);
        }
    }
}